#include "MainWidget.h"
#include "fastboot/FastbootThread.h"
#include <QSettings>
#include <QIcon>
#include <QFileDialog>
#include <QDebug>

extern int fastboot_main(int argc, char **argv);
MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent), ui(new Ui::MainWidget)
{
    ui->setupUi(this);

    fastboot = new FastbootThread(this);
    connect(fastboot, SIGNAL(deviceListChanged(QStringList)),
            this, SLOT(onDeviceListChanged(QStringList)));
    connect(fastboot, SIGNAL(logMessage(QString)),
            ui->logEdit, SLOT(appendPlainText(QString)));

    fastboot->start();

    on_deviceList_currentRowChanged(-1);

    loadSetting();
}

MainWidget::~MainWidget()
{
    fastboot->stop();
}

void MainWidget::setSetting(const QString &key, const QVariant &value)
{
    QSettings settings(QSettings::IniFormat,
                       QSettings::UserScope,
                       "SunplusAPP",
                       "qfastboot");
    settings.setValue(key, value);
    settings.sync();
}

QVariant MainWidget::getSetting(const QString &key, const QVariant &defValue)
{
    QSettings settings(QSettings::IniFormat,
                       QSettings::UserScope,
                       "SunplusAPP",
                       "qfastboot");
    return settings.value(key, defValue);
}

void MainWidget::loadSetting()
{
    ui->bootloaderGroup->setChecked(
                getSetting("bootloaderCheck", true).toBool());
    ui->bootloaderPathEdit->setText(
                getSetting("lastBootloaderPath").toString());

    ui->kernelGroup->setChecked(
                getSetting("kernelCheck", true).toBool());
    ui->kernelPathEdit->setText(
                getSetting("lastKernelPath").toString());

    ui->ramdiskGroup->setChecked(
                getSetting("ramdiskCheck", true).toBool());
    ui->ramdiskPathEdit->setText(
                getSetting("lastRamdiskPath").toString());

    ui->systemGroup->setChecked(
                getSetting("systemCheck", true).toBool());
    ui->systemPathEdit->setText(
                getSetting("lastSystemPath").toString());

    ui->userdataGroup->setChecked(
                getSetting("userdataCheck", false).toBool());
    ui->userdataPathEdit->setText(
                getSetting("lastUserdataPath").toString());

    ui->cacheGroup->setChecked(
                getSetting("cacheCheck", false).toBool());
    ui->cachePathEdit->setText(
                getSetting("lastCachePath").toString());
}

void MainWidget::onDeviceListChanged(const QStringList &list)
{
    ui->deviceList->clear();
    ui->deviceList->addItems(list);
    ui->deviceList->setCurrentRow(-1);
    for(int i = 0; i < ui->deviceList->count(); i++)
        ui->deviceList->item(i)->setIcon(QIcon(":/onebit_18.png"));
}

void MainWidget::on_deviceList_currentRowChanged(int currentRow)
{
    ui->opGroup->setEnabled(!fastboot->isBusy()
                            && ui->deviceList->currentRow() != -1);
}

void MainWidget::on_browseBootloaderBtn_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(
                this, tr("Select a image file"),
                getSetting("lastBootloaderPath").toString());
    if(filePath.isEmpty())
        return;
    ui->bootloaderPathEdit->setText(filePath);
    setSetting("lastBootloaderPath", filePath);
}

void MainWidget::on_browseKernelBtn_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(
                this, tr("Select a image file"),
                getSetting("lastKernelPath").toString());
    if(filePath.isEmpty())
        return;
    ui->kernelPathEdit->setText(filePath);
    setSetting("lastKernelPath", filePath);
}

void MainWidget::on_browseRamdiskBtn_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(
                this, tr("Select a image file"),
                getSetting("lastRamdiskPath").toString());
    if(filePath.isEmpty())
        return;
    ui->ramdiskPathEdit->setText(filePath);
    setSetting("lastRamdiskPath", filePath);
}

void MainWidget::on_browseSystemBtn_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(
                this, tr("Select a image file"),
                getSetting("lastSystemPath").toString());
    if(filePath.isEmpty())
        return;
    ui->systemPathEdit->setText(filePath);
    setSetting("lastSystemPath", filePath);
}

void MainWidget::on_browseUserdataBtn_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(
                this, tr("Select a image file"),
                getSetting("lastUserdataPath").toString());
    if(filePath.isEmpty())
        return;
    ui->userdataPathEdit->setText(filePath);
    setSetting("lastUserdataPath", filePath);
}

void MainWidget::on_browseCacheBtn_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(
                this, tr("Select a image file"),
                getSetting("lastCachePath").toString());
    if(filePath.isEmpty())
        return;
    ui->cachePathEdit->setText(filePath);
    setSetting("lastCachePath", filePath);
}

void MainWidget::on_bootloaderGroup_clicked(bool checked)
{
    setSetting("bootloaderCheck", checked);
}

void MainWidget::on_kernelGroup_clicked(bool checked)
{
    setSetting("kernelCheck", checked);
}

void MainWidget::on_ramdiskGroup_clicked(bool checked)
{
    setSetting("ramdiskCheck", checked);
}

void MainWidget::on_systemGroup_clicked(bool checked)
{
    setSetting("systemCheck", checked);
}

void MainWidget::on_userdataGroup_clicked(bool checked)
{
    setSetting("userdataCheck", checked);
}

void MainWidget::on_cacheGroup_clicked(bool checked)
{
    setSetting("cacheCheck", checked);
}

void MainWidget::on_writeBootloaderBtn_clicked()
{
    if(fastboot->isBusy())
        return;
    if(ui->bootloaderPathEdit->text().isEmpty())
        return;
    if(ui->deviceList->currentItem() == NULL)
        return;
    QString serialNumber = ui->deviceList->currentItem()->text();
    fastboot->writeImage(serialNumber, "bootloader",
                         ui->bootloaderPathEdit->text());
}

void MainWidget::on_writeKernelBtn_clicked()
{
    if(fastboot->isBusy())
        return;
    if(ui->kernelPathEdit->text().isEmpty())
        return;
    if(ui->deviceList->currentItem() == NULL)
        return;
    QString serialNumber = ui->deviceList->currentItem()->text();
    fastboot->writeImage(serialNumber, "kernel",
                         ui->kernelPathEdit->text());
}

void MainWidget::on_writeRamdiskBtn_clicked()
{
    if(fastboot->isBusy())
        return;
    if(ui->ramdiskPathEdit->text().isEmpty())
        return;
    if(ui->deviceList->currentItem() == NULL)
        return;
    QString serialNumber = ui->deviceList->currentItem()->text();
    fastboot->writeImage(serialNumber, "ramdisk",
                         ui->ramdiskPathEdit->text());
}

void MainWidget::on_writeSystemBtn_clicked()
{
    if(fastboot->isBusy())
        return;
    if(ui->systemPathEdit->text().isEmpty())
        return;
    if(ui->deviceList->currentItem() == NULL)
        return;
    QString serialNumber = ui->deviceList->currentItem()->text();
    fastboot->writeImage(serialNumber, "system",
                         ui->systemPathEdit->text());
}

void MainWidget::on_writeUserdataBtn_clicked()
{
    if(fastboot->isBusy())
        return;
    if(ui->userdataPathEdit->text().isEmpty())
        return;
    if(ui->deviceList->currentItem() == NULL)
        return;
    QString serialNumber = ui->deviceList->currentItem()->text();
    fastboot->writeImage(serialNumber, "userdata",
                         ui->userdataPathEdit->text());
}

void MainWidget::on_writeCacheBtn_clicked()
{
    if(fastboot->isBusy())
        return;
    if(ui->cachePathEdit->text().isEmpty())
        return;
    if(ui->deviceList->currentItem() == NULL)
        return;
    QString serialNumber = ui->deviceList->currentItem()->text();
    fastboot->writeImage(serialNumber, "cache",
                         ui->cachePathEdit->text());
}

void MainWidget::on_batchWriteBtn_clicked()
{
    if(fastboot->isBusy())
        return;
    if(ui->deviceList->currentItem() == NULL)
        return;
    QString serialNumber = ui->deviceList->currentItem()->text();
    if(ui->bootloaderGroup->isChecked())
        fastboot->writeImage(serialNumber, "bootloader",
                             ui->bootloaderPathEdit->text());
    if(ui->kernelGroup->isChecked())
        fastboot->writeImage(serialNumber, "kernel",
                             ui->kernelPathEdit->text());
    if(ui->ramdiskGroup->isChecked())
        fastboot->writeImage(serialNumber, "ramdisk",
                             ui->ramdiskPathEdit->text());
    if(ui->systemGroup->isChecked())
        fastboot->writeImage(serialNumber, "system",
                             ui->systemPathEdit->text());
    if(ui->userdataGroup->isChecked())
        fastboot->writeImage(serialNumber, "userdata",
                             ui->userdataPathEdit->text());
    if(ui->cacheGroup->isChecked())
        fastboot->writeImage(serialNumber, "cache",
                             ui->cachePathEdit->text());
}

void MainWidget::on_wrapBtn_clicked()
{
    if(fastboot->isBusy())
        return;
    if(ui->deviceList->currentItem() == NULL)
        return;
    QString serialNumber = ui->deviceList->currentItem()->text();
    fastboot->wrapSection(serialNumber, "userdata");
    fastboot->wrapSection(serialNumber, "cache");
}

void MainWidget::on_rebootDeviceBtn_clicked()
{
    if(fastboot->isBusy())
        return;
    if(ui->deviceList->currentItem() == NULL)
        return;
    QString serialNumber = ui->deviceList->currentItem()->text();
    fastboot->rebootDevice(serialNumber);
}
